home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / hough.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  2.4 KB  |  100 lines

  1. #include <stdio.h>
  2. #include "defs.h"
  3.  
  4. #define BLACK    0
  5. #define DEG2RAD    (M_PI/180.)
  6.  
  7. har           *progname;
  8. har           *filename;
  9. ixrect        *pr1, *pr2;
  10.  
  11. #ifdef STANDALONE
  12. ain(argc, argv, envp)
  13. #else
  14. ough_main(argc, argv, envp)
  15. #endif
  16.     int             argc;
  17.     char          **argv;
  18.     char          **envp;
  19. {
  20.     register int    i, j;
  21.     int             levels;
  22.     Pixrect *hough();
  23.  
  24.     progname = strsave(argv[0]);
  25.     parse_profile(&argc, argv, envp);
  26.  
  27.     while ((gc = getopt(argc, argv, " ")) != EOF) 
  28.         switch (gc) {
  29.         case '?':
  30.             errflag++;
  31.             break;
  32.         }
  33.  
  34.     if (errflag)
  35.         error((char *) 0, "Usage: %s: [infile] [outfile]\n", progname);
  36.  
  37.     for (stream = 0; optind < argc; stream++, optind++)
  38.         if (stream < 2 && strcmp(argv[optind], "-") != 0)
  39.             if (freopen(argv[optind], mode[stream], f[stream]) == NULL)
  40.                 error("%s %s", PR_IO_ERR_INFILE, argv[optind]);
  41.  
  42.     if ((pr1 = pr_load(stdin, NULL)) == NULL)
  43.         error(PR_IO_ERR_RASREAD);
  44.  
  45.     if (pr1->pr_depth != 1)
  46.         error("Input image must be 1 bit deep");
  47.  
  48.     pr2 = hough(pr1);
  49.  
  50.     pr_dump(pr2, stdout, NULL, RT_STANDARD, 0);
  51. }
  52.  
  53.  
  54.  
  55. ixrect *hough (pr)
  56. ixrect *pr;
  57. {
  58.     double sqrt ();                        /* square root function */
  59.     Pixrect *hp;                        /* new image for hough transformation */
  60.     int xoff, yoff;                        /* centre image = origin hough space */
  61.     register int r, theta;                /* coords in hough space */
  62.     register int x, y;                    /* coords in image */
  63.     int maxr;                            /* max val of r possible for image */
  64.     int c[180], s[180];                    /* lookup tables for cos and sin */
  65.  
  66.     /*  initialise variables  */
  67.  
  68.     xoff = pr->pr_size.x/2;
  69.     yoff = pr->pr_size.y/2;
  70.     maxr = (int) (sqrt ((double) (pr->pr_size.x * pr->pr_size.x + pr->pr_size.y * pr->pr_size.y)) / 2);
  71.  
  72.     for (theta = 0; theta < 180; theta++) {
  73.         c[theta] = (int) (cos (DEG2RAD * theta) * 1000);
  74.         s[theta] = (int) (sin (DEG2RAD * theta) * 1000);
  75.     }
  76.  
  77.     /*  create new image  */
  78.  
  79.     if ((hp = mem_create (180, 2 * maxr + 1, 8)) == NULL)
  80.         error("mem_create returned NULL");
  81.  
  82.     for (r = 0; r < 2 * maxr + 1; r++)
  83.         for (theta = 0; theta < 180; theta++)
  84.             pr_put(hp, theta, r, 0);
  85.  
  86.     /*  step through image  */
  87.  
  88.     for (y = 0; y < pr->pr_size.y; y++)
  89.         for (x = 0; x < pr->pr_size.x; x++)
  90.             if (pr_get(pr, x,y) == BLACK)
  91.                 for (theta = 0; theta < 180; ++theta) {
  92.                     r = (y - yoff) * s[theta] - (x - xoff) * c[theta];
  93.                     r = r < 0 ? r / 1000 : r / 1000 + 1;
  94.                     pr_put(hp, theta, maxr + r, pr_get(hp, theta, maxr + r) +1);
  95.                 }
  96.  
  97.     return (hp);
  98. }
  99.  
  100.